package com.zhongxi.guoyuan.view.beanExchange;


import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.OffLineShop.UI.OffLineBaseActivity;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.fragment.beanexchange.ExchangeNoEvaluateFragment;
import com.zhongxi.guoyuan.fragment.beanexchange.ExchangeNoPayFragment;
import com.zhongxi.guoyuan.fragment.beanexchange.ExchangeNoReceiveFragment;
import com.zhongxi.guoyuan.fragment.beanexchange.ExchangeNoSendFragment;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.view.HtmlActivity;

import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PersonExchangeActivity extends OffLineBaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.ivUserHead)
    ImageView mIvUserHead;
    @Bind(R.id.tvUserName)
    TextView mTvUserName;
    @Bind(R.id.tvUserPhone)
    TextView mTvUserPhone;
    @Bind(R.id.tvBonusPoint)
    TextView mTvBonusPoint;
    @Bind(R.id.tvIdentify)
    TextView mTvIdentify;
    @Bind(R.id.tab0)
    Button mTab0;
    @Bind(R.id.tab1)
    Button mTab1;
    @Bind(R.id.tab2)
    Button mTab2;
    @Bind(R.id.tab3)
    Button mTab3;

    @Bind(R.id.frame_container)
    FrameLayout mFrameContainer;

    ExchangeNoPayFragment mNoPayFragment;
    ExchangeNoSendFragment mNoSendFragment;
    ExchangeNoReceiveFragment mNoReceiveFragment;
    ExchangeNoEvaluateFragment mNoEvaluateFragment;


    private Fragment[] mFragments;
    private Button[] mButtons;
    private int currentIndex,index;

    public static List<Activity> activityList = new LinkedList<Activity>();
    @Override
    protected void init() {
        setContentView(R.layout.activity_person_exchange);
        ButterKnife.bind(this);
        initData();
    }

    @Override
    protected void initActionBar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationIcon(R.mipmap.topback);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

    }

    @Override
    protected void initView() {
        super.initView();
        mNoPayFragment=new ExchangeNoPayFragment();
        mNoSendFragment=new ExchangeNoSendFragment();
        mNoReceiveFragment=new ExchangeNoReceiveFragment();
        mNoEvaluateFragment=new ExchangeNoEvaluateFragment();
        mFragments=new Fragment[]{mNoPayFragment,mNoSendFragment,mNoReceiveFragment,mNoEvaluateFragment};
        mButtons=new Button[]{mTab0,mTab1,mTab2,mTab3};

        mButtons[0].setSelected(true);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.frame_container,mFragments[0])
                .commit();
    }

    @Override
    protected void initNetWork() {

    }
   private void initData(){
       if (TextUtils.isEmpty(MyApplication.getInstance().getInnerCurrency())) {
           mTvIdentify.setText("0 美豆");
       } else {
           mTvIdentify.setText(MyApplication.getInstance().getInnerCurrency() + " 美豆");
       }
       if (TextUtils.isEmpty(MyApplication.getInstance().getHdCurrency())) {
           mTvBonusPoint.setText("0 红豆");
       } else {
           mTvBonusPoint.setText(DataUtil.formatDataSixPoint(MyApplication.getInstance().getHdCurrency()) + " 红豆");
       }
       if (null != MyApplication.getInstance().getUserName() && !"".equals(MyApplication.getInstance().getUserName())) {
           mTvUserName.setText(MyApplication.getInstance().getUserName());
       } else {
           mTvUserName.setText(R.string.app_name);
       }
       if (null != MyApplication.getInstance().getPhone() && !"".equals(MyApplication.getInstance().getPhone())) {
           mTvUserPhone.setText(MyApplication.getInstance().getPhone());
       } else {
           mTvUserPhone.setText("");
       }

       Glide.with(this)
               .load(Url.BASE_URL + MyApplication.getInstance().getAvatar())
               .placeholder(R.mipmap.logo)
//                .transform(new GlideRoundTransform(context, 20))
               .into(mIvUserHead);
    }
    @Override
    protected void initListener() {
      mToolbar.setNavigationOnClickListener(this);
    }

    @OnClick({R.id.tab0, R.id.tab1, R.id.tab2, R.id.tab3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tab0:
                index=0;
                changeFragment();
                break;
            case R.id.tab1:
                index=1;
                changeFragment();
                break;
            case R.id.tab2:
                index=2;
                changeFragment();
                break;
            case R.id.tab3:
                index=3;
                changeFragment();
                break;
        }
    }
    private void changeFragment(){
        if (currentIndex!=index){
            FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
            mTransaction.hide(mFragments[currentIndex]);
            if (!mFragments[index].isAdded()){
                mTransaction.add(R.id.frame_container,mFragments[index]);
            }
            mTransaction.show(mFragments[index]);
            mTransaction.commit();
        }
        mButtons[currentIndex].setSelected(false);
        mButtons[index].setSelected(true);
        currentIndex=index;
    }

    @Override
    public void onClick(View v) {
        onBackPressed();
        finish();
    }
}
